classdef ziServerDaqModuleData < EquipmentControl.ZI.ziServer.ziServerData.ziServerData
    %ZISERVERPLOTTERDATA �˴���ʾ�йش����ժ�?
    %   �˴���ʾ��ϸ˵��
    
    properties
        domain
    end
    
    methods
        function obj = ziServerDaqModuleData(measured_data, nodeCell, domain)
            %ZISERVERPLOTTERDATA ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            obj@EquipmentControl.ZI.ziServer.ziServerData.ziServerData('ziDaqData', measured_data, nodeCell);
            obj.domain = domain;
        end
    end
    %%
    methods
        function enbw = getENBW(obj, device, ziNode)
            node = ziNode.node; signal = lower(ziNode.toSignalString); idx = Miscellaneous.ziIndexNum(ziNode.index);
            switch lower(ziNode.domain)
                case 'frequency'
                    dataStruct = obj.measured_data{1}.(device).(node)(idx).(signal){1};
                    df = dataStruct.header.gridcoldelta;
                    nenbw = dataStruct.header.nenbw;
                    enbw = df*nenbw;
                case 'time'
                    enbw = [];
            end
        end
        
        function data = getNodeData(obj, varargin)
            p=inputParser;
            p.addOptional('index', 1, @(x) ismember(x, 1:size(obj.data_node, 1)));
            p.parse(varargin{:});
            
            k = p.Results.index;
            node_k = obj.data_node(k, :);
            [data.x, data.y] = obj.get_data(node_k{1, [1 2]});
        end
        
        function enbw = getNodeENBW(obj, k)
            if nargin == 1
                k = 1;
            end
            node_k = obj.data_node(k, :);
            enbw = obj.getENBW(node_k{1, [1 2]});
        end
    end
    %%
    methods
        function [xList, yList] = get_data(obj, device, ziNode)
            clockbase = 60e6; xList = []; yList = [];
            if ~isempty(obj.measured_data)
                res = cellfun(@(data_k) obj.get_data_piece(data_k, device, ziNode), obj.measured_data);
                switch lower(ziNode.domain)%obj.domain
                    case 'time'
                        timestampsList = [res.timestamps]; 
                        xList = double(timestampsList-timestampsList(1))/clockbase;
                        [xList, idx]=unique(xList);
                        yList = [res.value];
                        yList = yList(idx);
                    case 'frequency'
                        xList = res.frequency;
                        yList = [res.value];                        
                end
            end
        end
        
        function res = get_data_piece(~, data_piece, device, ziNode)
            node = ziNode.node; signal = lower(ziNode.toSignalString); idx = Miscellaneous.ziIndexNum(ziNode.index);
            switch lower(ziNode.domain)%obj.domain
                case 'time'
                    switch node
                        case 'demods'
                            res.timestamps = cell2mat(cellfun(@(x) x.timestamp, data_piece.(device).(node)(idx).(signal), 'UniformOutput',false));
                            res.value = cell2mat(cellfun(@(x) x.value, data_piece.(device).(node)(idx).(signal), 'UniformOutput',false));
                        case 'pids'
                            signal1 = strsplit(signal, '/'); signal2 = signal1{2};
                            res.timestamps = cell2mat(cellfun(@(x) x.timestamp, data_piece.(device).(node)(idx).stream.(signal2), 'UniformOutput',false));
                            res.value = cell2mat(cellfun(@(x) x.value, data_piece.(device).(node)(idx).stream.(signal2), 'UniformOutput',false));
                    end
                case 'frequency'
                    switch node
                        case 'demods'
                            dataStruct = data_piece.(device).(node)(idx).(signal){1};
                            ncol_1 = dataStruct.header.gridcols-1;
                            df = dataStruct.header.gridcoldelta; 
                            res.frequency = df*double(0:ncol_1); % BUG: 这里可能导致frequency 跟 value的长度不一样
                            res.value = cell2mat(cellfun(@(x) x.value, data_piece.(device).(node)(idx).(signal), 'UniformOutput',false));
                        case 'pids'
                            signal1 = strsplit(signal, '/'); signal2 = signal1{2};
                            dataStruct = data_piece.(device).(node)(idx).stream.(signal2){1};
                            ncol_1 = dataStruct.header.gridcols-1;
                            df = dataStruct.header.gridcoldelta; 
                            res.frequency = df*double(0:ncol_1);  % BUG: 这里可能导致frequency 跟 value的长度不一样
                            res.value = cell2mat(cellfun(@(x) x.value, data_piece.(device).(node)(idx).stream.(signal2), 'UniformOutput',false));
                    end
            end
        end
        
        function plot_data(obj, device, ziNode, group, fig_hdl, color)
            if nargin == 3
                group = [1 1 1];
                fig_hdl = gcf;
                color = 1;
            elseif nargin == 4
                fig_hdl = gcf;
                color = 1;
            elseif nargin == 5
                color = 1;
            else
                %error
            end
                        
            [xList, yList] = obj.get_data(device, ziNode);
            
            ax = subplot(group(1), group(2), group(3), 'Parent', fig_hdl, 'XGrid', 'on', 'YGrid', 'on');
            colorVal = ax.ColorOrder(mod(color,7)+1, :);
            
            plot(ax, xList, yList, 'color', colorVal, 'LineStyle', '-', 'Marker', '.'); 
            xlim(ax, minmax(xList));
            if strcmpi(ziNode.domain, 'frequency')
                ax.YScale = 'log';
                xlabel(ax, 'Frequency (Hz)');
            else
                ax.YScale = 'linear';
                xlabel(ax, 'Time (s)');
            end
            grid(ax, 'on'); 
            
            legend(['/' device '/' ziNode.toDispString], 'location', 'best');
        end
        
        function plot_data_group(obj, fig_hdl)
            L = log4m.getLogger;
            if nargin == 1
                fig_hdl = gcf;
            end
            
            group = cell2mat(obj.data_node(:, 3)); 
            maxIndex = max(group, [], 1); nRow = maxIndex(1); nCol = maxIndex(2);
            subplot_list = unique(group(:, 3));
            
            for k = 1:length(subplot_list)
                ax_k = subplot_list(k);
                nodes = find(group(:, 3) == ax_k);
                ax = subplot(nRow, nCol, ax_k, 'Parent', fig_hdl);                
                
                for q = 1:length(nodes)
                    node_q = nodes(q);
                    device_q = obj.data_node{node_q, 1}; ziNode_q = obj.data_node{node_q, 2};

                    [xList, yList] = obj.get_data(device_q, ziNode_q);
                    yList = yList(1:length(xList));   % 临时补丁，待删除。 GeorgeGate@20200612
                    plot(ax, xList, yList, 'color', ax.ColorOrder(mod(node_q,7)+1, :),...
                                           'LineStyle', '-', 'Marker', '.');

                    try
                        xlim(ax, minmax(xList));
                    catch
                        L.trace('ziServerDaqModuleData::plot_data_group', sprintf('xlim [%f,] not set.', xList(:)));
                    end
                    if strcmpi(ziNode_q.domain, 'frequency')
                        ax.YScale = 'log';
                        xlabel(ax, 'Frequency (Hz)');
                    else
                        ax.YScale = 'linear';
                        xlabel(ax, 'Time (s)');
                    end
                    grid(ax, 'on');
                    hold(ax, 'on');
                end
                
                lgdString = arrayfun(@(node_q) ['/' obj.data_node{node_q, 1} '/' obj.data_node{node_q, 2}.toDispString], nodes, 'UniformOutput',false);
                legend(ax, lgdString, 'location', 'best');
                
                hold(ax, 'off');
            end
        end
    end
end

